#!/usr/bin/python
# -*- coding: UTF-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plot
import pyecharts as pc
import re
#指定默认字体
plot.style.use('ggplot')
plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family']='sans-serif'

def filter_birth(str):
    year=1
    result=re.compile(r".*?:(.*?)-.*?").match(str)
    if result:
        ss=result.group(1)
        if len(ss)==2:
            ss="20"+ss
        year_int=int(ss)
        if year_int<1980:
            year=7
        elif year_int<1990:
            year=8
        elif year_int<2000:
            year=9
        else:
            year=0

    return year


def filter_gender(str):
    gender="未知"
    result = re.compile(r".*?:(.*?)']").match(str)
    if result:
        gender=result.group(1)
        gender=gender.split(" ")[0]
    return gender



def main():
    data=pd.read_csv("xzpq.csv",encoding="utf-8")
    print(data.head())



def location_weibo(data):
    data["location"] = data["location"].apply(filter_gender)
    data = data.groupby(by="location").size()
    location = data.sort_values(ascending=False)
    location = location.head(16)
    bar = pc.Bar("关注小猪佩奇用户所在地区")
    bar.add("", location.index, location.values, xaxis_rotate=30)
    bar.render()
    print(location)


def gender(data):
    data["gender"] = data["gender"].apply(filter_gender)
    data = data.groupby(by="gender").size()
    pie = pc.Pie("关注小猪佩奇用户性别分布")
    pie.add("", data.index, data.values, is_label_show=True)
    pie.render()
    print(data)


def year(data):
    data["birth"] = data["birth"].apply(filter_birth)
    data = data.groupby(by="birth").size()
    per = list(data.values)
    lables = ["00后", "未知", "80年以前", "80后", "90后"]
    pie = pc.Pie("关注小猪佩奇用户年龄分布")
    pie.add("", lables, per, is_label_show=True)
    pie.render()
    print(data)


if __name__ == '__main__':
    main()